package com.yuncheng.spcyApi.spcy;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.bo.DbrBo;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.spcyApi.entity.SpcyJcyTxypjpsq;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.service.ISpcyJcyTxypjpsqService;
import com.yuncheng.spcyApi.service.ISpcyRsHcyService;
import com.yuncheng.spcyApi.service.ISpcyRsJcysfbgjlService;
import com.yuncheng.spcyApi.service.IYcSysUserService;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.GetKsKz;
import com.yuncheng.spcyApi.vo.common.HttpResultVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.common.StartEndStringVo;
import com.yuncheng.spcyApi.vo.jcy.JcyTxypSqVo;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @author hua
 */
@Api(tags = "检查员退休延聘申请管理-接口")
@RestController
@RequestMapping(value = "/api/spzx-spcy/jcyypsq")
public class JcyTxypSqController {

    @Resource
    private ISpcyJcyTxypjpsqService spcyJcyTxypjpsqService;

    @Resource
    private IYcSysUserService sysUserService;

    @Resource
    private ISpcyRsHcyService spcyRsHcyService;

    @Resource
    private ISpcyRsJcysfbgjlService spcyRsJcysfbgjlService;

    @Resource
    @Lazy
    private ZdyUploadFileService zdyUploadFileService;

    /*@Resource
    @Lazy
    private ZdyUploadFileService zdyUploadFileService;

    @Resource
    private ISpcyLogService spcyLogService;*/

    /** 查询 延聘 or 解聘 审核人 */
    private DbrBo getBlr(String sqlx){
        String dbrId = "";
        String dbr = "";

        String gw = "";
        if (sqlx.equals(SpcyCommonConstant.TXYPJP_SQLX_YP)){
            gw = SpcyConstant.GW_TXYPSQSHR;
        }else if (sqlx.equals(SpcyCommonConstant.TXYPJP_SQLX_JP)){
            gw = SpcyConstant.GW_JPSQSHR;
        }
        List<GetKsKz> ksKzObjectList = sysUserService.getKsKzObject("", gw);
        if (CollectionUtil.isNotEmpty(ksKzObjectList)) {
            dbrId = ksKzObjectList.get(0).getId();
            dbr = ksKzObjectList.get(0).getRealname();
        }

        return new DbrBo(dbrId, dbr);
    }

    /** 初始化-延聘解聘申请 */
    private SpcyJcyTxypjpsq initMyYpsqOrJpsq(String sqlx){
        YcSysUser user = sysUserService.getById(GetCurrentUserUtils.getCurrentUserId());
        if (user == null) {
            throw new BusinessException("当前用户不存在！");
        }
        SpcyJcyTxypjpsq sq = new SpcyJcyTxypjpsq();
        sq.setId(GetUuIdUtils.ReplaceUuId());
        sq.setFuserid(user.getId());
        sq.setRealname(user.getRealname());
        sq.setFgzdw(user.getFgzdw());
        sq.setFsfzh(user.getFsfzh());
        sq.setSex(user.getSex());
        sq.setFfqsj(new Date());

        String yjcylb = ""; // 原检查员类别
        if (sqlx.equals(SpcyCommonConstant.TXYPJP_SQLX_JP)) {
            List<SpcyRsHcy> jcyList = spcyRsHcyService.selectJcyYxList(user.getId());
            yjcylb = getJcyLbString(jcyList);

        }else if (sqlx.equals(SpcyCommonConstant.TXYPJP_SQLX_YP)){
            List<SpcyRsHcy> jcyList = spcyRsHcyService.selectJcyAllList(user.getId());
            yjcylb = getJcyLbString(jcyList);
        }
        sq.setFyjcylb(yjcylb);
        sq.setFsqlx(sqlx);
        sq.setFshzt(SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC);

        // 获取审核人
        DbrBo blr = getBlr(sqlx);
        sq.setFblrid(blr.getDbrId());
        sq.setFblr(blr.getDbr());
        sq.setFsfycl(SpcyCommonConstant.TXYPJP_SQ_SFYCL_NO);

        spcyJcyTxypjpsqService.save(sq);
        return sq;
    }

    private String getJcyLbString(List<SpcyRsHcy> jcyList){
        String yjcylb = "";
        if (CollectionUtil.isNotEmpty(jcyList)) {
            for (SpcyRsHcy jcy : jcyList) {
                if (StringUtils.isNotBlank(jcy.getFlx())
                        && StringUtils.isNotBlank(yjcylb)
                        && jcy.getFlx().equals(JcydjbConstant.LB_YPZCSC)
                        && yjcylb.contains(JcydjbConstant.LB_YPZCSC)){

                }else {
                    if (StringUtils.isBlank(yjcylb)) {
//                            yjcylb += jcy.getFjcyxl();
                        yjcylb += jcy.getFlx();
                    } else {
//                            yjcylb += "," + jcy.getFjcyxl();
                        yjcylb += "," + jcy.getFlx();
                    }
                }
            }
        }

        return yjcylb;
    }

    @Log(title = "延聘解聘-分页查询")
    @ApiOperation(value = "延聘解聘-分页查询")
    @GetMapping(value = "queryYpjpPageList")
    public HttpResult queryYpjpPageList(SpcyJcyTxypjpsq sq, String gjz, PageVo pageVo, OrderItemVo orderItemVo, StartEndStringVo timeVo){

        Page<SpcyJcyTxypjpsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyJcyTxypjpsq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sq.getRealname()), SpcyJcyTxypjpsq::getRealname, sq.getRealname());
        queryWrapper.eq(StringUtils.isNotBlank(sq.getFsfzh()), SpcyJcyTxypjpsq::getFsfzh, sq.getFsfzh());
        queryWrapper.eq(StringUtils.isNotBlank(sq.getFgzdw()), SpcyJcyTxypjpsq::getFgzdw, sq.getFgzdw());
        queryWrapper.eq(SpcyJcyTxypjpsq::getFsqlx, SpcyCommonConstant.TXYPJP_SQLX_YP);
        queryWrapper.eq(SpcyJcyTxypjpsq::getFblrid, GetCurrentUserUtils.getCurrentUserId());

        if (StringUtils.isNotBlank(gjz)){
            queryWrapper
                    .and(f->f.like(SpcyJcyTxypjpsq::getRealname, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsfzh, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFgzdw, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFnqdjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsftysq, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFshzt, gjz)
                    );
        }

        queryWrapper.ne(SpcyJcyTxypjpsq::getFshzt, SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC);

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(SpcyJcyTxypjpsq::getFfqsj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SpcyJcyTxypjpsq> pageList = spcyJcyTxypjpsqService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    @Log(title = "解聘-分页查询")
    @ApiOperation(value = "解聘-分页查询")
    @GetMapping(value = "queryJpPageList")
    public HttpResult queryJpPageList(SpcyJcyTxypjpsq sq, String gjz, PageVo pageVo, OrderItemVo orderItemVo, StartEndStringVo timeVo){

        Page<SpcyJcyTxypjpsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyJcyTxypjpsq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sq.getRealname()), SpcyJcyTxypjpsq::getRealname, sq.getRealname());
        queryWrapper.eq(StringUtils.isNotBlank(sq.getFsfzh()), SpcyJcyTxypjpsq::getFsfzh, sq.getFsfzh());
        queryWrapper.eq(StringUtils.isNotBlank(sq.getFgzdw()), SpcyJcyTxypjpsq::getFgzdw, sq.getFgzdw());
        queryWrapper.eq(SpcyJcyTxypjpsq::getFsqlx, SpcyCommonConstant.TXYPJP_SQLX_JP);
        queryWrapper.eq(SpcyJcyTxypjpsq::getFblrid, GetCurrentUserUtils.getCurrentUserId());

        if (StringUtils.isNotBlank(gjz)){
            queryWrapper
                    .and(f->f.like(SpcyJcyTxypjpsq::getRealname, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsfzh, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFgzdw, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFnqdjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsftysq, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFshzt, gjz)
                    );
        }

        queryWrapper.ne(SpcyJcyTxypjpsq::getFshzt, SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC);

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(SpcyJcyTxypjpsq::getFfqsj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SpcyJcyTxypjpsq> pageList = spcyJcyTxypjpsqService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    @Log(title = "我的延聘解聘历史-分页查询")
    @ApiOperation(value = "我的延聘解聘历史-分页查询")
    @GetMapping(value = "queryYpjpLsPageList")
    public HttpResult queryYpjpLsPageList(String gjz, PageVo pageVo, OrderItemVo orderItemVo){

        Page<SpcyJcyTxypjpsq> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcyJcyTxypjpsq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyJcyTxypjpsq::getFuserid, GetCurrentUserUtils.getCurrentUserId());
        queryWrapper.ne(SpcyJcyTxypjpsq::getFshzt, SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC);

        if (StringUtils.isNotBlank(gjz)){
            queryWrapper
                    .and(f->f.like(SpcyJcyTxypjpsq::getRealname, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsfzh, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFgzdw, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFnqdjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFyjcylb, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFsftysq, gjz)
                            .or()
                            .like(SpcyJcyTxypjpsq::getFshzt, gjz)
                    );
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(SpcyJcyTxypjpsq::getFfqsj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SpcyJcyTxypjpsq> pageList = spcyJcyTxypjpsqService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    @Log(title = "查询我的暂存-延聘信息")
    @ApiOperation(value = "查询我的暂存-延聘信息")
    @GetMapping(value = "queryWdZcypxx")
    public HttpResult queryWdZcypxx(){

        SpcyJcyTxypjpsq sq = null;

        List<SpcyJcyTxypjpsq> wshList = spcyJcyTxypjpsqService.selectYpsqListInWsh(GetCurrentUserUtils.getCurrentUserId());
        if (CollectionUtil.isEmpty(wshList)){
            sq = initMyYpsqOrJpsq(SpcyCommonConstant.TXYPJP_SQLX_YP);
        }else {
            sq = wshList.get(0);

            if (StringUtils.isNotBlank(sq.getFshzt()) && sq.getFshzt().equals(SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC)) {
                String yjcylb = ""; // 原检查员类别
                List<SpcyRsHcy> jcyList = spcyRsHcyService.selectJcyAllList(sq.getFuserid());
                yjcylb = getJcyLbString(jcyList);

                sq.setFyjcylb(yjcylb);
                sq.setFfqsj(new Date());

                if (StringUtils.isNotBlank(sq.getFuserid())) {
                    YcSysUser user = sysUserService.getById(sq.getFuserid());
                    if (user != null) {
                        sq.setRealname(user.getRealname());
                        sq.setFgzdw(user.getFgzdw());
                        sq.setFsfzh(user.getFsfzh());
                        sq.setSex(user.getSex());
                    }
                }

                spcyJcyTxypjpsqService.updateById(sq);
            }

        }

        Map resultMap = new HashMap<>();
        resultMap.put("sq", sq);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "查询我的暂存-解聘信息")
    @ApiOperation(value = "查询我的暂存-解聘信息")
    @GetMapping(value = "queryWdZcjpxx")
    public HttpResult queryWdZcjpxx(){

        SpcyJcyTxypjpsq sq = null;

        List<SpcyJcyTxypjpsq> wshList = spcyJcyTxypjpsqService.selectJpsqListInWsh(GetCurrentUserUtils.getCurrentUserId());
        if (CollectionUtil.isEmpty(wshList)){
            sq = initMyYpsqOrJpsq(SpcyCommonConstant.TXYPJP_SQLX_JP);
        }else {
            sq = wshList.get(0);

            if (StringUtils.isNotBlank(sq.getFshzt()) && sq.getFshzt().equals(SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC)) {
                String yjcylb = ""; // 原检查员类别
                List<SpcyRsHcy> jcyList = spcyRsHcyService.selectJcyYxList(sq.getFuserid());
                yjcylb = getJcyLbString(jcyList);

                sq.setFyjcylb(yjcylb);
                sq.setFfqsj(new Date());

                if (StringUtils.isNotBlank(sq.getFuserid())) {
                    YcSysUser user = sysUserService.getById(sq.getFuserid());
                    if (user != null) {
                        sq.setRealname(user.getRealname());
                        sq.setFgzdw(user.getFgzdw());
                        sq.setFsfzh(user.getFsfzh());
                        sq.setSex(user.getSex());
                    }
                }

                spcyJcyTxypjpsqService.updateById(sq);
            }
        }

        Map resultMap = new HashMap<>();
        resultMap.put("sq", sq);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "查询当前申请信息")
    @ApiOperation(value = "查询当前申请信息")
    @GetMapping(value = "querySqInfo")
    public HttpResult querySqInfo(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        SpcyJcyTxypjpsq sq = spcyJcyTxypjpsqService.getById(id);
        if (sq == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        return HttpResult.ok(sq);
    }

    @Log(title = "保存申请-暂存")
    @ApiOperation(value = "保存申请-暂存")
    @PostMapping(value = "bcsqZc")
    public HttpResult bcsqZc(@RequestBody SpcyJcyTxypjpsq bean) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (StringUtils.isBlank(bean.getId())){
            return HttpResult.error("参数为空！");
        }

        SpcyJcyTxypjpsq updateBean = spcyJcyTxypjpsqService.getById(bean.getId());
        if (updateBean == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, BeanUtils.describe(bean));
        spcyJcyTxypjpsqService.updateById(updateBean);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "提交申请")
    @ApiOperation(value = "提交申请")
    @PostMapping(value = "submitSq")
    public HttpResult submitSq(@RequestBody JcyTxypSqVo submitVo) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        SpcyJcyTxypjpsq sq = spcyJcyTxypjpsqService.getById(submitVo.getId());
        if (sq == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        if (StringUtils.isBlank(sq.getFsqjcylb())){
            return HttpResult.error("提交失败，没有可申请解聘的类型！");
        }

        if (StringUtils.isNotBlank(sq.getFshzt()) && !sq.getFshzt().equals(SpcyCommonConstant.TXYPJP_SQ_SHZT_ZC)){
            return HttpResult.error("已经提交，无法重复提交！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        Integer sex = sq.getSex();
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(sq, BeanUtils.describe(submitVo));
        sq.setSex(sex);
        sq.setFshzt(SpcyCommonConstant.TXYPJP_SQ_SHZT_WSH);
        spcyJcyTxypjpsqService.updateById(sq);

        return HttpResult.ok("提交成功");
    }

    @Log(title = "提交审核")
    @ApiOperation(value = "提交审核")
    @PostMapping(value = "submitSh")
    public HttpResult submitSh(@RequestBody JcyTxypSqVo submitVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        if (currentUser == null){
            return HttpResult.error("当前登录已失效，请重新登录！");
        }
        if (StringUtils.isBlank(submitVo.getSfty())){
            return HttpResult.error("参数为空！");
        }
        SpcyJcyTxypjpsq sq = spcyJcyTxypjpsqService.getById(submitVo.getId());
        if (sq == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        if (StringUtils.isNotBlank(sq.getFsfycl()) && sq.getFshzt().equals(SpcyCommonConstant.TXYPJP_SQ_SFYCL_YES)){
            return HttpResult.error("该数据已经处理，无法提交！");
        }

        if (StringUtils.isNotBlank(sq.getFshzt()) && sq.getFshzt().equals(SpcyCommonConstant.TXYPJP_SQ_SHZT_YSH)){
            return HttpResult.error("已经提交，无法重复提交！");
        }

        if (submitVo.getSfty().equals("是") || submitVo.getSfty().equals("true")){
            sq.setFsftysq(SpcyCommonConstant.TXYPJP_SQ_SFTYSQ_YES);
        }else {
            sq.setFsftysq(SpcyCommonConstant.TXYPJP_SQ_SFTYBQ_NO);
        }

        sq.setFpq(submitVo.getFpq());
        sq.setFprjsrq(submitVo.getFprjsrq());
        sq.setFblyj(submitVo.getOpinion());
        sq.setFblsj(new Date());
        sq.setFshzt(SpcyCommonConstant.TXYPJP_SQ_SHZT_YSH);
        spcyJcyTxypjpsqService.updateById(sq);


        shtgBySfbg(sq, sq.getFsftysq());


        return HttpResult.ok("提交成功");
    }

    /** 审核通过 - 身份变更 */
    private void shtgBySfbg(SpcyJcyTxypjpsq sq, String sftysq){
        if (sq.getFsftysq().equals(SpcyCommonConstant.TXYPJP_SQ_SFTYSQ_YES)){
            if (StringUtils.isNotBlank(sq.getFsqlx()) && sq.getFsqlx().equals(SpcyCommonConstant.TXYPJP_SQLX_YP)){
                shtgBySfbgInSqTxyp(sq);
            }else if (StringUtils.isNotBlank(sq.getFsqlx()) && sq.getFsqlx().equals(SpcyCommonConstant.TXYPJP_SQLX_JP)){
                shtgBySfbgInSqJp(sq);
            }
        }
    }

    /** 审核通过 - 身份变更- 退休延聘 */
    private void shtgBySfbgInSqTxyp(SpcyJcyTxypjpsq sq){
        if (StringUtils.isNotBlank(sq.getFsqjcylb())){
            List<String> sqlbList = StringPatternUtils.getListByIds(sq.getFsqjcylb());

            for (String lb : sqlbList){
                /*List<SpcyRsHcy> jcyList = spcyRsHcyService.list(new LambdaQueryWrapper<SpcyRsHcy>()
                        .eq(SpcyRsHcy::getFjcyxl, lb)
                        .eq(SpcyRsHcy::getFuserid, sq.getFuserid()));*/
                List<SpcyRsHcy> jcyList = spcyRsHcyService.list(new LambdaQueryWrapper<SpcyRsHcy>()
                        .eq(SpcyRsHcy::getFlx, lb)
                        .eq(SpcyRsHcy::getFuserid, sq.getFuserid()));
                if (CollectionUtil.isNotEmpty(jcyList)){
                    for (SpcyRsHcy updateJcy : jcyList) {
//                        SpcyRsHcy updateJcy = jcyList.get(0);

                        if (StringUtils.isNotBlank(updateJcy.getFstate()) && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_ZX)) {
                            updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_YX);
                            updateJcy.setFypjssj(sq.getFprjsrq());
                            spcyRsHcyService.updateById(updateJcy);

                            spcyRsJcysfbgjlService.initSqTxypJcysgbgjl(sq.getFuserid(), updateJcy.getId(), updateJcy.getFlx(), updateJcy.getFjcyxl(), updateJcy.getFssk(), updateJcy.getFszly(), sq.getId(), false);
                        }
                    }
                }
            }

            // 更新有效状态
            sysUserService.updateUserGzztInZg(sq.getFuserid());
        }
    }

    /** 审核通过 - 身份变更- 解聘 */
    private void shtgBySfbgInSqJp(SpcyJcyTxypjpsq sq){

        boolean isUserGzzt = false;
        if (StringUtils.isBlank(sq.getFyjcylb())){
            isUserGzzt = true;
        }

        if (StringUtils.isNotBlank(sq.getFsqjcylb())){
            List<String> sqlbList = StringPatternUtils.getListByIds(sq.getFsqjcylb());

            for (String lb : sqlbList){
                /*List<SpcyRsHcy> jcyList = spcyRsHcyService.list(new LambdaQueryWrapper<SpcyRsHcy>()
                        .eq(SpcyRsHcy::getFjcyxl, lb)
                        .eq(SpcyRsHcy::getFuserid, sq.getFuserid()));*/
                List<SpcyRsHcy> jcyList = spcyRsHcyService.list(new LambdaQueryWrapper<SpcyRsHcy>()
                        .eq(SpcyRsHcy::getFlx, lb)
                        .eq(SpcyRsHcy::getFuserid, sq.getFuserid()));
                if (CollectionUtil.isNotEmpty(jcyList)){
                    for (SpcyRsHcy updateJcy : jcyList) {
//                        SpcyRsHcy updateJcy = jcyList.get(0);
                        if (StringUtils.isNotBlank(updateJcy.getFstate()) && updateJcy.getFstate().equals(SpcyCommonConstant.JCY_STATE_YX)) {
                            updateJcy.setFstate(SpcyCommonConstant.JCY_STATE_ZX);
                            updateJcy.setFspjssj(sq.getFtxsj());
                            spcyRsHcyService.updateById(updateJcy);

                            spcyRsJcysfbgjlService.initSqJpJcysgbgjl(sq.getFuserid(), updateJcy.getId(), updateJcy.getFlx(), updateJcy.getFjcyxl(), updateJcy.getFssk(), updateJcy.getFszly(), sq.getId(), false);
                        }
                    }
                }
            }

            if (!isUserGzzt){
                List<String> ylbList = StringPatternUtils.getListByIds(sq.getFyjcylb());
                List<String> existList = new ArrayList<>();
                for (String ylb : ylbList){
                    if (sqlbList.contains(ylb)){
                        existList.add("是");
                    }else {
                        existList.add("否");
                    }
                }

                if (!existList.contains("否")){
                    isUserGzzt = true;
                }
            }
        }

        if (isUserGzzt) {
            // 更新有效状态
            sysUserService.updateUserGzztInJp(sq.getFuserid());
        }
    }

    @Log(title = "上传文件-申请文件")
    @ApiOperation(value = "上传文件-申请文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file",value = "文件不能为空",required = true),
            @ApiImplicitParam(name = "id",value = "申请id",required = true)
    })
    @PostMapping(value = "/uploadFile")
    public HttpResult<?> uploadFile(@RequestPart("file") MultipartFile[] uploadFile,
                                    @RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("上传失败，申请参数为空！");
        }

        SpcyJcyTxypjpsq bgsq = spcyJcyTxypjpsqService.getById(id);
        if (bgsq == null){
            return HttpResult.error("没有相匹配的数据！");
        }

        List<SpcySqsxWjjl> wjjlList = zdyUploadFileService.queryWjjlByPid(id);
        if (CollectionUtil.isNotEmpty(wjjlList)){
            for (SpcySqsxWjjl wjjl : wjjlList) {
                zdyUploadFileService.deleteFileRecords(wjjl);
                zdyUploadFileService.deleteFileByUrl(wjjl.getFcflj());
            }
        }

        String wjid = GetUuIdUtils.ReplaceUuId();
        zdyUploadFileService.uploadFile(uploadFile, id, LogConstant.LOGS_JCY_TXYPJPSQ, SpcyConstant.JCY_YPJPSQ_FILE_BASE_URL+"/"+ DateConversionUtils.DateToYear(new Date()), wjid);

        String wjHttpUrl = zdyUploadFileService.genWjllHttpUrl(wjid);
        bgsq.setFfjdz(wjHttpUrl);
        spcyJcyTxypjpsqService.updateById(bgsq);

        return HttpResultVo.HttpResultSave(bgsq,"上传成功");
    }

}
